<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>牛奶信息</title>
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>奶牛管理<small>奶牛信息</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>奶牛管理</el-breadcrumb-item>
                    <el-breadcrumb-item>奶牛信息</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="奶牛相关信息" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                        <el-button @click="findPage()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column prop="code" align="center" label="编号"></el-table-column>
                        <el-table-column prop="type" label="类种" align="center"></el-table-column>
                        <el-table-column prop="yieldAge" label="产龄/月" align="center"></el-table-column>
                        <el-table-column prop="totalYield" label="总产量/ml" align="center"></el-table-column>
                        <el-table-column prop="status" label="身体状况" align="center"></el-table-column>
                        <el-table-column prop="roomId" label="舍号" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <br>
                                <hr>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增奶牛" :visible.sync="dialogFormVisible">
                            <el-form ref="dataForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="编号" prop="code">
                                            <el-input v-model="formData.code"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="类种" prop="type">
                                            <el-input v-model="formData.type"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-col :span="12">
                                    <el-form-item label="产龄/月" prop="yieldAge">
                                        <el-input v-model="formData.yieldAge"/>
                                    </el-form-item>
                                </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="产量/ml" prop="totalYield">
                                            <el-input  v-model="formData.totalYield"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="身体状况" prop="status">
                                            <el-input v-model="formData.status"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="舍号" prop="roomId">
                                            <el-input  v-model="formData.roomId"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handle()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                pagination: {//分页相关模型数据
					  currentPage: 1,//当前页码
					  pageSize:10,//每页显示的记录数
					  total:0,//总记录数
					  queryString:''//查询条件
				},
				dataList: [{id:'1',code:'0003',type:'马尔西牛',yieldAge:3,totalYield:300,status:'良好',roomId:'203'},
                    {id:'1',code:'0003',type:'马尔西牛',yieldAge:3,totalYield:300,status:'良好',roomId:'203'},
                    {id:'1',code:'0003',type:'马尔西牛',yieldAge:3,totalYield:300,status:'良好',roomId:'203'},
                    {id:'1',code:'0003',type:'马尔西牛',yieldAge:3,totalYield:300,status:'良好',roomId:'203'},
                    {id:'1',code:'0003',type:'马尔西牛',yieldAge:3,totalYield:300,status:'良好',roomId:'203'}],//当前页要展示的分页列表数据
                formData: {},//表单数据
                dialogFormVisible: false,//增加表单是否可见
                // dialogFormVisible4Edit:false,//编辑表单是否可见
                rules: {//校验规则
                    type: [{ required: true, message: '类别为必填项', trigger: 'blur' }],
                    roomId: [{ required: true, message: '舍号为必填项', trigger: 'blur' }],
                    code: [{ required: true, message: '编号为必填项', trigger: 'blur' }]
                },
                /*标记添加还是修改*/
                flag:''
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                this.findPage();
            },
            methods: {
                handle(){
                    if(this.flag=='add'){
                        this.handleAdd();
                    }else if(this.flag=='edit'){
                        this.handleEdit();
                    }
                },
                //编辑
                handleEdit(){
                    this.$refs['dataForm'].validate((valid)=>{
                        if(valid){
                            axios.put("/cow",this.formData).then((res)=>{
                                if(res.data.flag){
                                    this.$message.success(res.data.message);
                                    this.findPage();
                                }else{
                                    this.$message.error(res.data.message);
                                }
                                this.dialogFormVisible=false;
                            });
                        }else {
                            this.$message.info("数据校验失败!!!");
                        }
                    });
                },
                //添加
                handleAdd () {
                    this.$refs['dataForm'].validate((valid)=>{
                       if(valid){
                           axios.post("/cow",this.formData).then((res)=>{
                               if(res.data.flag){
                                   this.$message.success(res.data.message);
                                   this.findPage()
                               }else {
                                   this.$message.error(res.data.message);
                               }
                               this.dialogFormVisible=false;
                           });
                       } else{
                           this.$message.info("数据校验失败!!!");
                       }
                    });
                },
                //分页查询
                findPage() {
                    var param={
                        currentPage:this.pagination.currentPage,
                        pageSize:this.pagination.pageSize,
                        queryString:this.pagination.queryString
                    };
                    axios.post("/cow/queryPage",param).then((res)=>{
                         this.pagination.total=res.data.total;
                         this.dataList=res.data.rows;
                    }).catch((res)=>{
                        this.$message.error("权限不足");
                    });
                },
                // 重置表单
                resetForm() {
                    this.formData={};
                },
                // 弹出添加窗口
                handleCreate() {
                    this.flag='add';
                    this.resetForm();
                    /*初始化一些固定数据*/
                    this.formData.status='优秀',
                    this.formData.yieldAge=0,
                    this.formData.totalYield=0,
                    this.dialogFormVisible=true;
                },
                // 弹出编辑窗口
                handleUpdate(row) {
                    this.flag='edit';
                    this.formData=row;
                    this.dialogFormVisible=true;
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    this.pagination.currentPage=currentPage;
                    this.findPage();
                },
                showMessage(r){
                    if(r == 'Error: Request failed with status code 403'){
                        //权限不足
                        this.$message.error('无访问权限');
                        return;
                    }else{
                        this.$message.error('未知错误');
                        return;
                    }
                },
                // 删除
                handleDelete(row) {
                    this.$confirm("是否确定删除?","请谨慎删除",{type:'warning'}).then((flag)=>{
                        if(flag){
                            axios.delete("/cow/"+row.id).then((res)=>{
                                if(res.data.flag){
                                    this.findPage();
                                    this.$message.success(res.data.message);
                                }else{
                                    this.$message.error(res.data.message);
                                }
                            }).catch((res=>{
                                this.showMessage(res);
                            }));
                        }else {
                            this.$message.info("已取消！！！");
                        }
                    });
                }
            }
        })
    </script>
</html>